<template>
    <div class="permission-list-container">
        <!-- 页面头部 -->
        <div class="page-header">
            <div class="header-content">
                <div class="header-left">
                    <el-icon class="header-icon">
                        <List />
                    </el-icon>
                    <h2 class="page-title">权限列表</h2>
                </div>
                <div class="header-right">
                    <el-button type="primary" @click="refreshData">
                        <el-icon>
                            <Refresh />
                        </el-icon>
                        刷新
                    </el-button>
                    <el-button type="success" @click="goToAddPermission">
                        <el-icon>
                            <Plus />
                        </el-icon>
                        添加权限
                    </el-button>
                </div>
            </div>
        </div>

        <!-- 搜索区域 -->
        <div class="search-section">
            <el-card class="search-card">
                <el-form :model="searchForm" inline>
                    <el-form-item label="权限名称：">
                        <el-input v-model="searchForm.permissonName" placeholder="请输入权限名称" clearable
                            style="width: 300px" @keyup.enter="handleSearch" />
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" @click="handleSearch">
                            <el-icon>
                                <Search />
                            </el-icon>
                            搜索
                        </el-button>
                        <el-button @click="handleReset">
                            <el-icon>
                                <RefreshLeft />
                            </el-icon>
                            重置
                        </el-button>

                    </el-form-item>
                </el-form>
            </el-card>
        </div>

        <!-- 表格区域 -->
        <div class="table-section">
            <el-card class="table-card">
                <el-table :data="user" v-loading="loading" stripe style="width: 100%"
                    :header-cell-style="{ background: '#f5f7fa', color: '#606266' }">
                    <el-table-column type="index" label="序号" width="80" align="center" />
                    <el-table-column prop="permissonName" label="权限名称" align="center" min-width="150">
                        <template #default="scope">
                            <el-tag type="primary" effect="light">
                                {{ scope.row.permissonName }}
                            </el-tag>
                        </template>
                    </el-table-column>
                    <el-table-column prop="permissonValue" label="权限路径" align="center" min-width="200">
                        <template #default="scope">
                            <el-text type="info" class="path-text">
                                {{ scope.row.permissonValue }}
                            </el-text>
                        </template>
                    </el-table-column>
                    <el-table-column prop="perseId" label="父级ID" align="center" width="100">
                        <template #default="scope">
                            <el-tag v-if="scope.row.perseId" type="success" size="small">
                                {{ scope.row.perseId }}
                            </el-tag>
                            <el-tag v-else type="info" size="small">
                                根节点
                            </el-tag>
                        </template>
                    </el-table-column>
                    <el-table-column label="操作" align="center" width="200" fixed="right">
                        <template #default="scope">
                            <div class="action-buttons">
                                <div class="button-row">
                                    <el-button type="primary" size="small" @click="mx(scope.row.permissonId)"
                                        class="action-btn edit-btn">
                                        <el-icon>
                                            <Edit />
                                        </el-icon>
                                        编辑
                                    </el-button>
                                    <el-button type="danger" size="small" @click="handleDelete(scope.row)"
                                        class="action-btn delete-btn">
                                        <el-icon>
                                            <Delete />
                                        </el-icon>
                                        删除
                                    </el-button>
                                </div>
                            </div>
                        </template>
                    </el-table-column>
                </el-table>

                <!-- 分页 -->
                <div class="pagination-container">
                    <el-pagination v-model:current-page="currentPage" v-model:page-size="pageSize"
                        :page-sizes="[10, 20, 50, 100]" :total="total" layout="total, sizes, prev, pager, next, jumper"
                        @size-change="handleSizeChange" @current-change="handleCurrentChange" />
                </div>
            </el-card>
        </div>
    </div>
</template>

<script lang="ts" setup>
import { ref, reactive, onMounted } from 'vue'
import { read, write } from '@/Until/axiosHepler'
import { useCounterStore } from '@/stores/counter'
import { useRouter, useRoute } from 'vue-router'
import axios from 'axios'
import { ElMessage, ElMessageBox } from 'element-plus'
import {
    List,
    Refresh,
    Plus,
    Search,
    RefreshLeft,
    Edit,
    Delete
} from '@element-plus/icons-vue'

const stores = useCounterStore()
const route = useRoute()
const router = useRouter()

// 响应式数据
const user = ref([])
const loading = ref(false)
const currentPage = ref(1)
const pageSize = ref(10)
const total = ref(0)

// 搜索表单
const searchForm = reactive({
    permissonName: ''
})

// 获取权限列表数据
const fetchData = async () => {
    loading.value = true
    try {
        const response = await read.get("api/User/PermissonList", {
            params: {
                permissonName: searchForm.permissonName,
                page: currentPage.value,
                pageSize: pageSize.value
            }
        })
        user.value = response.data.data || []
        total.value = response.data.total || user.value.length
    } catch (error) {
        ElMessage.error('获取权限列表失败')
        console.error('Error fetching data:', error)
    } finally {
        loading.value = false
    }
}

const mx = (permissonId: any) => {
    router.push({
        path: '/MainView/PermissonDetail',
        query: { permissonId: permissonId }
    })
}
// 搜索
const handleSearch = () => {
    currentPage.value = 1
    fetchData()
}

// 重置搜索
const handleReset = () => {
    searchForm.permissonName = ''
    currentPage.value = 1
    fetchData()
}

// 刷新数据
const refreshData = () => {
    fetchData()
}

// 跳转到添加权限页面
const goToAddPermission = () => {
    router.push('/MainView/AddPermission')
}

// 编辑权限
const handleEdit = (row: any) => {
    router.push(`/MainView/EditPermission/${row.id || 1}`)
}

// 删除权限
const handleDelete = async (row: any) => {
    try {
        await ElMessageBox.confirm(
            `确定要删除权限 "${row.permissonName}" 吗？`,
            '确认删除',
            {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
            }
        )
        write.post('api/Manger/DelPermisson', row).then((res => {
            if (res.data.code == 200) {
                ElMessage.success('删除成功')
                fetchData()
            }
        }))
        // 这里调用删除API

    } catch {
        ElMessage.info('已取消删除')
    }
}

// 分页处理
const handleSizeChange = (val: number) => {
    pageSize.value = val
    currentPage.value = 1
    fetchData()
}

const handleCurrentChange = (val: number) => {
    currentPage.value = val
    fetchData()
}

// 组件挂载时获取数据
onMounted(() => {
    fetchData()
})
</script>
<style scoped>
.permission-list-container {
    padding: 20px;
    background: #f5f7fa;
    min-height: 100vh;
}

/* 页面头部样式 */
.page-header {
    margin-bottom: 20px;
}

.header-content {
    display: flex;
    justify-content: space-between;
    align-items: center;
    background: white;
    padding: 20px 24px;
    border-radius: 12px;
    box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
}

.header-left {
    display: flex;
    align-items: center;
    gap: 12px;
}

.header-icon {
    font-size: 24px;
    color: #409eff;
}

.page-title {
    margin: 0;
    font-size: 24px;
    font-weight: 600;
    color: #303133;
    background: linear-gradient(135deg, #409eff, #67c23a);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
}

.header-right {
    display: flex;
    gap: 12px;
}

/* 搜索区域样式 */
.search-section {
    margin-bottom: 20px;
}

.search-card {
    border-radius: 12px;
    box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
    border: none;
}

.search-card :deep(.el-card__body) {
    padding: 20px 24px;
}

/* 表格区域样式 */
.table-section {
    margin-bottom: 20px;
}

.table-card {
    border-radius: 12px;
    box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
    border: none;
}

.table-card :deep(.el-card__body) {
    padding: 0;
}

/* 表格样式 */
:deep(.el-table) {
    border-radius: 12px;
    overflow: hidden;
}

:deep(.el-table__header) {
    background: #f8f9fa;
}

:deep(.el-table th) {
    background: #f8f9fa !important;
    color: #606266;
    font-weight: 600;
    border-bottom: 2px solid #e4e7ed;
}

:deep(.el-table td) {
    border-bottom: 1px solid #f0f0f0;
}

:deep(.el-table--striped .el-table__body tr.el-table__row--striped td) {
    background: #fafafa;
}

:deep(.el-table__body tr:hover > td) {
    background: #f0f9ff !important;
}

/* 路径文本样式 */
.path-text {
    font-family: 'Monaco', 'Menlo', 'Ubuntu Mono', monospace;
    font-size: 12px;
    background: #f5f7fa;
    padding: 4px 8px;
    border-radius: 4px;
    border: 1px solid #e4e7ed;
}

/* 分页样式 */
.pagination-container {
    padding: 20px 24px;
    display: flex;
    justify-content: center;
    background: #fafbfc;
    border-top: 1px solid #e4e7ed;
}

:deep(.el-pagination) {
    justify-content: center;
}

:deep(.el-pagination .el-pager li) {
    border-radius: 6px;
    margin: 0 2px;
}

:deep(.el-pagination .el-pager li.is-active) {
    background: #409eff;
    color: white;
}

:deep(.el-pagination .btn-prev),
:deep(.el-pagination .btn-next) {
    border-radius: 6px;
}

/* 按钮样式优化 */
:deep(.el-button) {
    border-radius: 8px;
    font-weight: 500;
    transition: all 0.3s ease;
}

:deep(.el-button:hover) {
    transform: translateY(-1px);
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}

:deep(.el-button--primary) {
    background: linear-gradient(135deg, #409eff, #67c23a);
    border: none;
}

:deep(.el-button--success) {
    background: linear-gradient(135deg, #67c23a, #85ce61);
    border: none;
}

:deep(.el-button--danger) {
    background: linear-gradient(135deg, #f56c6c, #f78989);
    border: none;
}

/* 标签样式 */
:deep(.el-tag) {
    border-radius: 6px;
    font-weight: 500;
}

:deep(.el-tag--primary) {
    background: linear-gradient(135deg, #409eff, #67c23a);
    color: white;
    border: none;
}

:deep(.el-tag--success) {
    background: linear-gradient(135deg, #67c23a, #85ce61);
    color: white;
    border: none;
}

:deep(.el-tag--info) {
    background: linear-gradient(135deg, #909399, #b1b3b8);
    color: white;
    border: none;
}

/* 输入框样式 */
:deep(.el-input__wrapper) {
    border-radius: 8px;
    transition: all 0.3s ease;
}

:deep(.el-input__wrapper:hover) {
    box-shadow: 0 0 0 1px #409eff;
}

:deep(.el-input__wrapper.is-focus) {
    box-shadow: 0 0 0 2px rgba(64, 158, 255, 0.2);
}

/* 卡片样式 */
:deep(.el-card) {
    border: none;
    box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
    transition: all 0.3s ease;
}

:deep(.el-card:hover) {
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.15);
}

/* 操作列按钮样式 */
.action-buttons {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 8px;
}

.button-row {
    display: flex;
    gap: 8px;
    align-items: center;
}

.action-btn {
    border-radius: 8px;
    font-weight: 500;
    transition: all 0.3s ease;
    border: none;
    min-width: 70px;
    height: 32px;
    font-size: 12px;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 4px;
}

.action-btn:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.2);
}

.edit-btn {
    background: linear-gradient(135deg, #409eff, #67c23a);
    color: white;
}

.edit-btn:hover {
    background: linear-gradient(135deg, #337ecc, #529b2e);
    box-shadow: 0 4px 12px rgba(64, 158, 255, 0.3);
}

.delete-btn {
    background: linear-gradient(135deg, #f56c6c, #f78989);
    color: white;
}

.delete-btn:hover {
    background: linear-gradient(135deg, #f23030, #f56c6c);
    box-shadow: 0 4px 12px rgba(245, 108, 108, 0.3);
}

.action-btn .el-icon {
    font-size: 14px;
}

/* 操作列标题样式 */
:deep(.el-table th:last-child) {
    background: #f8f9fa !important;
    color: #606266;
    font-weight: 600;
    border-bottom: 2px solid #e4e7ed;
}

/* 操作列单元格样式 */
:deep(.el-table td:last-child) {
    padding: 12px 8px;
    vertical-align: middle;
}

/* 响应式设计 */
@media (max-width: 768px) {
    .permission-list-container {
        padding: 10px;
    }

    .header-content {
        flex-direction: column;
        gap: 16px;
        align-items: stretch;
    }

    .header-right {
        justify-content: center;
    }

    .search-card :deep(.el-form) {
        flex-direction: column;
        align-items: stretch;
    }

    .search-card :deep(.el-form-item) {
        margin-right: 0;
        margin-bottom: 16px;
    }

    .search-card :deep(.el-input) {
        width: 100% !important;
    }

    .action-buttons {
        flex-direction: column;
        gap: 6px;
    }

    .button-row {
        flex-direction: column;
        gap: 6px;
    }

    .action-btn {
        min-width: 60px;
        height: 28px;
        font-size: 11px;
    }
}

@media (max-width: 480px) {
    .page-title {
        font-size: 20px;
    }

    .header-icon {
        font-size: 20px;
    }

    :deep(.el-table) {
        font-size: 12px;
    }

    .action-btn {
        padding: 6px 10px;
        font-size: 10px;
        min-width: 50px;
        height: 26px;
    }
}
</style>